home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / include / user / RCS / sync.h,v < prev    next >
Encoding:
Text File  |  1992-07-17  |  10.8 KB  |  398 lines

  1. head     1.7;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.7 srv027:1.7 srv026:1.7 srv024:1.7 srv021:1.7 srv019:1.7 srv018:1.7 srv016:1.7 srv014:1.7 srv010:1.7 srv008:1.7 srv007:1.7 srv006:1.6 srv005:1.6 srv004:1.6 srv003:1.6 srv002:1.6 srv001:1.6;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.7
  10. date     92.05.27.21.19.32;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.6;
  13.  
  14. 1.6
  15. date     92.04.02.18.45.26;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.5;
  18.  
  19. 1.5
  20. date     91.11.11.23.12.39;  author kupfer;  state Exp;
  21. branches ;
  22. next     1.4;
  23.  
  24. 1.4
  25. date     91.09.24.19.47.19;  author kupfer;  state Exp;
  26. branches ;
  27. next     1.3;
  28.  
  29. 1.3
  30. date     91.08.30.16.10.03;  author kupfer;  state Exp;
  31. branches ;
  32. next     1.2;
  33.  
  34. 1.2
  35. date     91.08.15.18.33.56;  author kupfer;  state Exp;
  36. branches ;
  37. next     1.1;
  38.  
  39. 1.1
  40. date     91.08.07.11.29.33;  author kupfer;  state Exp;
  41. branches ;
  42. next     ;
  43.  
  44.  
  45. desc
  46. @@
  47.  
  48.  
  49. 1.7
  50. log
  51. @Add dynamicName flag to Sync_Condition.
  52. @
  53. text
  54. @/*
  55.  * sync.h --
  56.  *
  57.  *     Typedefs and macros to support the use of monitors.
  58.  *      Clients use the macros to acquire and release monitor locks, and
  59.  *      to wait on and notify monitored condition variables.  Only Broadcast
  60.  *      semantics are supported for lock release; that is, all processes
  61.  *      waiting on a lock are made runnable when the locked is released.
  62.  *
  63.  * Copyright 1986, 1988, 1991 Regents of the University of California
  64.  * Permission to use, copy, modify, and distribute this
  65.  * software and its documentation for any purpose and without
  66.  * fee is hereby granted, provided that the above copyright
  67.  * notice appear in all copies.  The University of California
  68.  * makes no representations about the suitability of this
  69.  * software for any purpose.  It is provided "as is" without
  70.  * express or implied warranty.
  71.  *
  72.  *
  73.  * $Header: /user5/kupfer/spriteserver/include/user/RCS/sync.h,v 1.6 92/04/02 18:45:26 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  74.  */
  75.  
  76. #ifndef _SYNCUSER
  77. #define _SYNCUSER
  78.  
  79. #include <sprite.h>
  80. #include <cthreads.h>
  81.  
  82. /* 
  83.  * Dummy definition for a user lock.  This is to keep the compiler happy 
  84.  * when dealing with programs that include Sprite server header files.  It
  85.  * needs to be fixed whenever we get around to supporting multi-threaded
  86.  * user programs.
  87.  */
  88.  
  89. typedef int Sync_UserLock;
  90.  
  91. #if !defined(KERNEL) && !defined(SPRITED)
  92. typedef Sync_UserLock Sync_Lock;
  93. #endif
  94.  
  95. /*
  96.  * Condition variables represent events that are associated with
  97.  * locks.  The operations on a condition variable are Sync_Wait and
  98.  * Sync_Broadcast. The lock must be acquired before a call to
  99.  * Sync_Wait is made.  The lock is released while a process waits on a
  100.  * condition, and is then re-acquired when the condition is notified
  101.  * via Sync_Broadcast.
  102.  * 
  103.  * For the Sprite server we basically just use a C Threads condition
  104.  * variable.  The "initialized" flag is used for compatibility with
  105.  * native Sprite code, which doesn't require that condition variables be 
  106.  * initialized before use.
  107.  */
  108.  
  109. typedef struct Sync_Condition {
  110.     struct condition condVar;
  111.     Boolean initialized;
  112.     Boolean dynamicName;    /* does c.v. name need freeing */
  113. } Sync_Condition;
  114.  
  115. /*
  116.  * Maximum number of prior locks that can exist for any type of lock. A prior
  117.  * lock is the last lock to be grabbed by the process grabbing the current 
  118.  * lock. Keeping track of the locks held by a process is done in the proc
  119.  * module. By knowing all the prior locks of a lock it is possible to 
  120.  * construct a tree representing the locking behavior of the system.
  121.  * SYNC_MAX_PRIOR places a limit on how many prior locks will be remembered
  122.  * for an individual lock.
  123.  */
  124.  
  125. #ifndef LOCKDEP
  126. #define SYNC_MAX_PRIOR 1
  127. #else
  128. #define SYNC_MAX_PRIOR 30
  129. #endif
  130.  
  131. /*
  132.  * This structure is used to pass locking statistics to user programs.
  133.  * One of these structures is needed per lock type.
  134.  */
  135. typedef struct {
  136.     int        inUse;                /* 1 if structure is valid  */
  137.     int        class;                /* 0 = semaphore, 1 = lock */
  138.     int        hit;                /* # times lock was grabbed */
  139.     int        miss;                /* # times lock was missed */
  140.     char    name[30];            /* name of lock */
  141.     int        priorCount;            /* # of prior locks  */
  142.     int        priorTypes[SYNC_MAX_PRIOR];    /* types of prior locks */
  143.     int        activeCount;            /* # active locks of type */
  144.     int        deadCount;            /* # dead locks of type */
  145.     int        spinCount;            /* # spins waiting for lock */
  146. } Sync_LockStat;
  147.  
  148.  
  149. /*
  150.  * ----------------------------------------------------------------------------
  151.  *
  152.  * Monitor Locks --
  153.  *
  154.  *    The following set of macros are used to emulate monitored
  155.  *    procedures of Mesa.  The Sprite style document has a complete
  156.  *    description of the conventions required to emulate a set
  157.  *    of monitored procedures; a summary is presented here.
  158.  *
  159.  *      The LOCK_MONITOR and UNLOCK_MONITOR macros depend on a constant
  160.  *      LOCKPTR.  LOCKPTR should be defined as the address of the lock
  161.  *      variable used to lock the monitor.  Something like the following
  162.  *      two lines of code should appear at the beginning of a file of
  163.  *      monitored procedures.
  164.  *
  165.  *    Sync_Lock modMonitorLock;
  166.  *    #define LOCKPTR (&modMonitorLock)
  167.  *
  168.  *    The pseudo-keywords INTERNAL and ENTRY denote internal and entry
  169.  *    procedures of a monitor.  INTERNAL procedures can only be called
  170.  *    when the monitor lock is held.  ENTRY procedures are procedures
  171.  *    that acquire the lock.  There may also be External procedures.
  172.  *    They are the default and there is no special keyword.  An External
  173.  *    procedure doesn't explicitly acquire the monitor lock, but may
  174.  *    call an ENTRY procedure.
  175.  *
  176.  * ----------------------------------------------------------------------------
  177.  */
  178.  
  179. /* XXX disable monitors for user code. */
  180. #ifdef SPRITED
  181. #define LOCK_MONITOR               (void) Sync_GetLock(LOCKPTR)
  182. #define UNLOCK_MONITOR             (void) Sync_Unlock(LOCKPTR)
  183. #else
  184. #define LOCK_MONITOR
  185. #define UNLOCK_MONITOR
  186. #endif
  187.  
  188. #define ENTRY
  189. #define INTERNAL
  190.  
  191.  
  192. /*
  193.  * ----------------------------------------------------------------------------
  194.  *
  195.  * Sync_Wait --
  196.  *
  197.  * Wait on a condition variable.  This can only be called while a lock
  198.  * is aquired because it is only safe to check global state while in a
  199.  * monitor.  This releases the monitor lock and makes the process sleep
  200.  * on the condition. The monitor lock is reacquired after the process 
  201.  * has been woken up.
  202.  *
  203.  * The address of the condition variable is used as the generic 'event'
  204.  * that the process blocks on.  The value of the condition variable is
  205.  * used to indicate if there is a process waiting on the condition.
  206.  *
  207.  * Results:
  208.  *    None.
  209.  *
  210.  * Side effects:
  211.  *    Put the process to sleep and release the monitor lock.
  212.  *
  213.  * ----------------------------------------------------------------------------
  214.  */
  215.  
  216. #define Sync_Wait(conditionPtr, wakeIfSignal) \
  217.     Sync_SlowWait(conditionPtr, LOCKPTR, wakeIfSignal)
  218.  
  219.  
  220. /*
  221.  * ----------------------------------------------------------------------------
  222.  *
  223.  * Sync_Broadcast --
  224.  *
  225.  *      Notify other processes that a condition has been met (this is 
  226.  *      the version for user processes only).
  227.  *
  228.  * Monitor Lock:
  229.  *    This routine needs to be called with the monitor lock held.
  230.  *    This ensures synchronous access to the conditionPtr->waiting flag.
  231.  *
  232.  * Results:
  233.  *    None.
  234.  *
  235.  * Side effects:
  236.  *    Make the processes waiting on the condition variable runnable.
  237.  *
  238.  * ----------------------------------------------------------------------------
  239.  */
  240.  
  241. #ifndef SPRITED
  242. #define Sync_Broadcast(conditionPtr) \
  243.     if (((Sync_Condition *)conditionPtr)->waiting == TRUE)  { \
  244.     (void)Sync_SlowBroadcast((unsigned int) conditionPtr, \
  245.       &((Sync_Condition *)conditionPtr)->waiting); \
  246.     }
  247. #endif
  248.  
  249. /*
  250.  * Exported procedures of the sync module for monitors.
  251.  */
  252.  
  253. #if !defined(KERNEL) && !defined(SPRITED)
  254. extern ReturnStatus    Sync_GetLock();
  255. extern ReturnStatus    Sync_Unlock();
  256. #endif
  257. extern ReturnStatus    Sync_SlowLock();
  258. extern ReturnStatus    Sync_SlowWait();
  259. extern ReturnStatus    Sync_SlowBroadcast();
  260.  
  261. #endif /* _SYNCUSER */
  262. @
  263.  
  264.  
  265. 1.6
  266. log
  267. @Add dummy definition for Sync_UserLock.
  268. @
  269. text
  270. @d20 1
  271. a20 1
  272.  * $Header: /user5/kupfer/spriteserver/include/user/RCS/sync.h,v 1.5 91/11/11 23:12:39 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  273. d59 1
  274. @
  275.  
  276.  
  277. 1.5
  278. log
  279. @Condition variables might not be initially bzero'd.  Also, be careful
  280. about conflicting user/server declarations for lock/unlock.
  281. @
  282. text
  283. @d20 1
  284. a20 1
  285.  * $Header: /r3/kupfer/spriteserver/include/user/RCS/sync.h,v 1.4 91/09/24 19:47:19 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  286. d28 13
  287. @
  288.  
  289.  
  290. 1.4
  291. log
  292. @Turn on condition variables.
  293. @
  294. text
  295. @d20 1
  296. a20 1
  297.  * $Header: /user6/kupfer/spriteserver/include/user/RCS/sync.h,v 1.3 91/08/30 16:10:03 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  298. d39 2
  299. a40 2
  300.  * native Sprite code that assumed that a zero'd Sync_Condition would
  301.  * be sufficiently initialized.
  302. d186 1
  303. d189 1
  304. @
  305.  
  306.  
  307. 1.3
  308. log
  309. @Disable monitors for user code.
  310. @
  311. text
  312. @d20 1
  313. a20 1
  314.  * $Header: /user6/kupfer/spriteserver/include/user/RCS/sync.h,v 1.2 91/08/15 18:33:56 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  315. a148 1
  316. #if 0
  317. a150 1
  318. #endif
  319. d158 2
  320. a159 4
  321.  *      Notify other processes that a condition has been met.  If the
  322.  *      condition variable indicates that there are processes waiting
  323.  *      on the condition, then Sync_SlowBroadcast is used to wakeup
  324.  *      waiting processes.
  325. d174 1
  326. a174 1
  327. #if 0
  328. @
  329.  
  330.  
  331. 1.2
  332. log
  333. @Initial changes for use on top of C Threads.
  334. @
  335. text
  336. @d20 1
  337. a20 1
  338.  * $Header: /user6/kupfer/spriteserver/include/user/RCS/sync.h,v 1.1 91/08/07 11:29:33 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)
  339. d112 2
  340. d116 4
  341. @
  342.  
  343.  
  344. 1.1
  345. log
  346. @Initial revision
  347. @
  348. text
  349. @d10 1
  350. a10 6
  351.  *      These macros do a fast check on the state of the lock or condition
  352.  *      variable to reduce overhead.  In the best case the lock and unlock
  353.  *      routines just set or clear flags.  In the worst case these routines
  354.  *      have to call slower routines that must acquire a master lock.
  355.  *
  356.  * Copyright 1986, 1988 Regents of the University of California
  357. d20 1
  358. a20 1
  359.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.7 90/01/16 17:59:49 jhh Exp $ SPRITE (Berkeley)
  360. d26 2
  361. a27 27
  362. #ifndef _SPRITE
  363. #include "sprite.h"
  364. #endif
  365.  
  366. /*
  367.  * The state of a lock consists of two bits of information, a lock bit
  368.  * and a waiting bit.  The lock bit indicates that someone has the
  369.  * lock, and no other process can acquire the lock until this bit is
  370.  * cleared.  The waiting bit indicates that someone wants the lock.
  371.  *
  372.  * The user locks are different from the kernel locks. If we are compiling
  373.  * user code then the Sync_UserLock type is typedef'ed to be Sync_Lock.
  374.  * The Sync_UserLock type is used by the kernel to differentiate between
  375.  * the two types of locks.
  376.  *
  377.  * Note: the inUse field is a whole word so it can be
  378.  *       an argument to the test-and-set instruction.
  379.  */
  380.  
  381. typedef struct Sync_UserLock {
  382.     Boolean inUse;        /* 1 while the lock is busy */
  383.     Boolean waiting;        /* 1 if someone wants the lock */
  384. } Sync_UserLock;
  385.  
  386. #ifndef KERNEL 
  387. typedef Sync_UserLock Sync_Lock;     /* define user locks */
  388. #endif
  389. d36 5
  390. d44 2
  391. a45 1
  392.     Boolean waiting;        /* 1 if someone is waiting on the condition */
  393. d143 1
  394. d146 1
  395. d172 1
  396. d178 1
  397. @
  398.